#include "ibp_util.h"

void QueryNetIpByHostName( char *hostname , char *ip , int ip_bufsize )
{
	struct hostent	*phe = NULL ;
	char		**ip_addr = NULL , *ip_ptr = NULL ;
	
	phe = gethostbyname( hostname ) ;
	if( phe == NULL )
	{
		snprintf( ip , ip_bufsize-1 , "%s" , hostname );
		return;
	}
	
	switch( phe->h_addrtype )
	{
		case AF_INET :
		case AF_INET6 :
			ip_addr = phe->h_addr_list ;
#ifdef inet_ntop
				inet_ntop( phe->h_addrtype, *ip_addr , ip , ip_bufsize );
#else
				ip_ptr = inet_ntoa( *(struct in_addr*)(*ip_addr) ) ;
				snprintf( ip , ip_bufsize-1 , "%s" , ip_ptr );
#endif
			
			break;
		default :
			snprintf( ip , ip_bufsize-1 , "%s" , hostname );
			break;
	}

	return;
}

void QueryNetPortByServiceName( char *servname , char *proto , int *port )
{
	struct servent	*pse = NULL ;
	
	pse = getservbyname( servname , proto ) ;
	if( pse == NULL )
	{
		(*port) = atoi(servname) ;
		return;
	}
	
	(*port) = (int)ntohs(pse->s_port) ;
	
	return;
}

